﻿using System;
using System.Data;
using System.Linq;
using BabyShop.WebSite.Models;

namespace BabyShop.WebSite.Services
{
    public class SerialNumberService : BaseService<SerialNumber>
    {
        public string GetSerialNumber(DateTime serialNumberDate, Guid modifiedBy, string serialNumberCode)
        {
            var currSerialNumber =
                DataServices.SerialNumbers.FirstOrDefault(p => p.SerialNumberDate == serialNumberDate &&
                                                               p.SerialNumberCode == serialNumberCode &&
                                                               p.ModifiedBy == modifiedBy);
            if (currSerialNumber != null && !currSerialNumber.IsInvalid)
                return serialNumberDate.ToString("yyyyMMdd") + currSerialNumber.CurrentNumber.ToString("00000");

            int? currNumber = DataServices.SerialNumbers.Where(p => p.SerialNumberDate == serialNumberDate &&
                                                                   p.SerialNumberCode == serialNumberCode).Max(
                                                                       p => ((int?)p.CurrentNumber));
            int nextNumber = (currNumber ?? 0) + 1;
            if (currSerialNumber != null && currSerialNumber.IsInvalid) {
                currSerialNumber.CurrentNumber = nextNumber;
                currSerialNumber.IsInvalid = false;
                DataServices.Entry(currSerialNumber).State = EntityState.Modified;
            }
            else
                DataServices.SerialNumbers.Add(new SerialNumber
                                                   {
                                                       CurrentNumber = nextNumber,
                                                       IsInvalid = false,
                                                       ModifiedBy = modifiedBy,
                                                       SerialNumberCode = serialNumberCode,
                                                       SerialNumberDate = serialNumberDate
                                                   });
            DataServices.SaveChanges();
            return serialNumberDate.ToString("yyyyMMdd") + nextNumber.ToString("00000");
        }

        public bool SetSerialNumberInValid(DateTime serialNumberDate, Guid modifiedBy, string serialNumberCode)
        {
            var currSerialNumber =
                DataServices.SerialNumbers.FirstOrDefault(p => p.SerialNumberDate == serialNumberDate &&
                                                               p.SerialNumberCode == serialNumberCode &&
                                                               p.ModifiedBy == modifiedBy);
            if(currSerialNumber != null) {
                currSerialNumber.IsInvalid = true;
                DataServices.Entry(currSerialNumber).State = EntityState.Modified;
                return DataServices.SaveChanges() > 0;
            }
            return false;
        }
    }
}